import request, { post, get } from './request';

function getFormId() {
  // eslint-disable-next-line no-unused-vars
  const [ _, __, params ] = window.location.pathname.split('/');
  return params;
}

export async function testapi(url, method, options = {}) {
  const config = {
    ...options,
    method,
  };
  return request(url, {
    config,
  });
}

export async function saveDesktopForm(data) {
  const id = getFormId();
  const url = `/node/form/updatePcFormJson`;
  return post(url, {
    data: { id, ...data },
  });
}

export async function saveMobileForm(data) {
  const id = getFormId();
  const url = `/node/form/updateMobileFormJson`;
  return post(url, {
    data: { id, ...data },
  });
}

export async function fetchForm() {
  const id = getFormId();
  const url = `/node/form/getOne/${id}`;
  return get(url);
}

export async function fetchAllForm(options = {}) {
  const url = `/node/form`;
  return get(url, options);
}

export async function loadfetchAllForm(data) {
  const url = `/node/form/insertOne`;
  return post(url, {
    data,
  });
}

export async function updateStateForm(id, state) {
  const url = `/node/form/updateState`;
  return post(url, {
    data: { id, state },
  });
}

export async function deleteForm(id) {
  const url = `/node/form/deleteForm`;
  return post(url, {
    data: { id },
  });
}

export async function loadType() {
  const url = `/node/type`;
  return get(url);
}

export async function deleteOneType(id) {
  const url = `/node/type/deleteOne`;
  return post(url, {
    data: { id },
  });
}

export async function insertOneType(name) {
  const url = `/node/type/insertOne`;
  return post(url, {
    data: { name },
  });
}

export async function getLicense() {
  const url = `/node/license`;
  return get(url);
}

export async function updateForm(data) {
  const id = getFormId();
  const url = `/node/form/updateForm/${id}`;
  return post(url, {
    data,
  });
}

export async function getCode() {
  const url = `/node/license/getMachineCode`;
  return get(url);
}

